package com.intellekt.ecert.exportacion.action;

import static com.intellekt.ecert.domain.Constantes.USUARIO;

import java.util.Map;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;

import net.sf.jasperreports.engine.JasperExportManager;

import org.apache.log4j.Logger;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.apache.struts2.interceptor.ServletResponseAware;
import org.apache.struts2.interceptor.SessionAware;
import org.springframework.beans.factory.annotation.Autowired;

import com.intellekt.ecert.domain.DomainActionSupport;
import com.intellekt.ecert.domain.UsuarioVO;
import com.intellekt.ecert.exportacion.service.ExportacionService;

@Results({ @Result(name = "input", location = "exportacion", type = "tiles") })
public class ExportacionAction extends DomainActionSupport implements SessionAware, ServletResponseAware {

	/**
     * 
     */
	private static final long serialVersionUID = -1915779054202062140L;
	private static final Logger log = Logger.getLogger(ExportacionAction.class);

	@Autowired
	private ExportacionService service;
	
	protected Map<String, Object> session;
	protected HttpServletResponse response;
	protected Map request;	
	private String idCert;
	
	@Override
	public String input() throws Exception {
		if (log.isInfoEnabled()) {
			log.info("input Parametros...");
		}

		String verify = verificar(this.getActionId(), (UsuarioVO) session.get(USUARIO));
		if (verify != null) {
			return verify;
		}

		return INPUT;
	}

	/*@SuppressWarnings("rawtypes")
	public void reporte() throws Exception {

		Map params = new HashMap();
		String[] columns = { "name", "address", "zipcode", "country" };

		params.put("REPORT_TITLE", "14-0000300000");
		
		DynamicReport dynamicReport = new DynamicReportBuilder().setTemplateFile(
				applicationContext.getResource("classpath:templates/exportacionTemplate.jrxml").getFile().getAbsolutePath()).build();
		
		JasperReport jr = DynamicJasperHelper.generateJasperReport(dynamicReport, new ClassicLayoutManager(), params);
		JasperPrint jp = JasperFillManager.fillReport(jr, params, new JREmptyDataSource());

		byte pdf[] = JasperExportManager.exportReportToPdf(jp);
		
		ServletOutputStream os = response.getOutputStream();

		os.write(pdf);
		response.setContentLength(pdf.length);
		os.flush();
		os.close();
		response.flushBuffer();

	}*/


	public void reporte() throws Exception {

		byte pdf[] = service.getPdf(idCert);;
		
		ServletOutputStream os = response.getOutputStream();

		os.write(pdf);
		response.setContentLength(pdf.length);
		os.flush();
		os.close();
		response.flushBuffer();

	}
	

	public void setRequest(@SuppressWarnings("rawtypes") Map request) {
		this.request = request;
	}
	
	public void setServletResponse(HttpServletResponse reponse) {
		this.response = reponse;
	}
	
	public void setSession(Map<String, Object> session) {
		this.session = session;
	}

	public String getIdCert() {
		return idCert;
	}

	public void setIdCert(String idCert) {
		this.idCert = idCert;
	}

	@Override
	public String getActionId() {
		return "exportacion";
	}

}
